/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.UI;

import br.com.Enumeradores.EstadosBotoes;
import br.com.business.ClienteBO;
import br.com.entities.ClienteBE;
import br.com.utils.MeuTableModel;
import br.com.utils.Utils;
import java.awt.HeadlessException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 * Tela/Camada de úsuario para CRUD de clientes
 *
 * @author Kevlin Toshinari Ossada - 30/01/2013 - kevlin.ossada@fatec.sp.gov.br
 */
public class PnlClientes extends javax.swing.JFrame {

    /**
     * Creates new form pnlClientes
     */
    public PnlClientes() {
        initComponents();
        habilitarBotoes(EstadosBotoes.INICIAL);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        lblId = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        edtNome = new javax.swing.JTextField();
        edtEndereco = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        edtBairro = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        edtCidade = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        edtTelefone = new javax.swing.JTextField();
        edtRg = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        btnCadastrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblClientes = new javax.swing.JTable();
        btnDeletar = new javax.swing.JButton();
        btnInserir = new javax.swing.JButton();
        edtCpf = new javax.swing.JFormattedTextField();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(627, 640));

        lblId.setText("999999");

        jLabel3.setText("CPF");

        jLabel2.setText("Nome");

        jLabel1.setText("ID");

        edtNome.setText("jTextField2");
        edtNome.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                edtNomeFocusGained(evt);
            }
        });
        edtNome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                edtNomeKeyPressed(evt);
            }
        });

        edtEndereco.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                edtEnderecoFocusGained(evt);
            }
        });

        jLabel4.setText("Endereço");

        edtBairro.setText("Jardim Paulistano");
        edtBairro.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                edtBairroFocusGained(evt);
            }
        });

        jLabel5.setText("Bairro");

        edtCidade.setText("Americana");
        edtCidade.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                edtCidadeFocusGained(evt);
            }
        });

        jLabel6.setText("Cidade");

        jLabel7.setText("Telefone");

        edtTelefone.setText("1934063005");
        edtTelefone.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                edtTelefoneFocusGained(evt);
            }
        });
        edtTelefone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                edtTelefoneKeyTyped(evt);
            }
        });

        edtRg.setText("49.627.116-7");
        edtRg.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                edtRgFocusGained(evt);
            }
        });
        edtRg.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                edtRgKeyTyped(evt);
            }
        });

        jLabel8.setText("RG");

        btnCadastrar.setText("CADASTRAR");
        btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarActionPerformed(evt);
            }
        });

        btnCancelar.setText("CANCELAR");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        tblClientes.setModel(new MeuTableModel (new Object [] [] {}, new Object [] {"ID", "Nome", "CPF", "Endereco", "Bairro", "Cidade", "Telefone", "Rg"}));
        tblClientes.getTableHeader().setReorderingAllowed(false);
        tblClientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblClientesMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tblClientes);

        btnDeletar.setText("DELETAR");
        btnDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletarActionPerformed(evt);
            }
        });

        btnInserir.setText("INSERIR");
        btnInserir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInserirActionPerformed(evt);
            }
        });

        edtCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        edtCpf.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                edtCpfFocusGained(evt);
            }
        });
        edtCpf.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                edtCpfKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3)
                    .addComponent(edtEndereco)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(edtBairro))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(edtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(edtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(edtRg, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 131, Short.MAX_VALUE)
                        .addComponent(btnInserir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDeletar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCadastrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel8))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(lblId))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(edtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 397, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(edtCpf))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblId)
                    .addComponent(edtCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(edtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(edtBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(edtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(edtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edtRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCadastrar)
                    .addComponent(btnCancelar)
                    .addComponent(btnDeletar)
                    .addComponent(btnInserir))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed

        habilitarBotoes(EstadosBotoes.CANCELAR);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarActionPerformed
        if (validarCampos()) {
            try {
                ClienteBE oCliente = new ClienteBE(
                        Integer.parseInt(lblId.getText()),
                        Integer.parseInt(edtCpf.getText()),
                        edtNome.getText(),
                        edtEndereco.getText(),
                        edtBairro.getText(),
                        edtCidade.getText(),
                        edtTelefone.getText(),
                        edtRg.getText());

                if (btnCadastrar.getText().equals(EstadosBotoes.CADASTRAR.toString())) {
                    if (ClienteBO.getInstance().inserirCliente(oCliente)) {
                        JOptionPane.showMessageDialog(null, "Inserido com sucesso!");
                    }
                } else if (btnCadastrar.getText().equals(EstadosBotoes.ALTERAR.toString())) {
                    if (ClienteBO.getInstance().alterarCliente(oCliente)) {
                        JOptionPane.showMessageDialog(null, "Alterado com sucesso!");
                    }
                }


                habilitarBotoes(EstadosBotoes.CADASTRAR);

            } catch (HeadlessException e) {
                JOptionPane.showMessageDialog(null, new StringBuilder().append("Erro ao tentar inserir! Erro: ").append(e.getMessage()).toString());
            }catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null, new StringBuilder().append("Favor preencher corretamente os campos numericos! Erro: ").append(e.getMessage()).toString());
            }
        } else {
            JOptionPane.showMessageDialog(null, "Preencher Campos obrigatorios!");
        }
    }//GEN-LAST:event_btnCadastrarActionPerformed

    private void tblClientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblClientesMouseClicked
        habilitarBotoes(EstadosBotoes.ALTERAR);
    }//GEN-LAST:event_tblClientesMouseClicked

    private void btnInserirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInserirActionPerformed
        habilitarBotoes(EstadosBotoes.INSERIR);
    }//GEN-LAST:event_btnInserirActionPerformed

    private void btnDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletarActionPerformed
        ClienteBE oCliente = new ClienteBE(
                Integer.parseInt(lblId.getText()),
                Integer.parseInt(edtCpf.getText()),
                edtNome.getText(),
                edtEndereco.getText(),
                edtBairro.getText(),
                edtCidade.getText(),
                edtTelefone.getText(),
                edtRg.getText());
        if (JOptionPane.showConfirmDialog(null, new StringBuilder().append("Tem certeza que deseja excluir o cliente ")
                .append(edtNome.getText()).toString(),
                "Exlusão de cliente",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            try {
                ClienteBO.getInstance().deletarCliente(oCliente);

                JOptionPane.showMessageDialog(null, new StringBuilder().append("Cliente: ").append(oCliente.getNomeCliente()).append(" Deletado com sucesso!").toString());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, new StringBuilder().append("Erro").append(e.getMessage()).toString());
            } finally {
                habilitarBotoes(EstadosBotoes.DELETAR);
            }

        }


    }//GEN-LAST:event_btnDeletarActionPerformed

    private void edtNomeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_edtNomeKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_edtNomeKeyPressed

    private void edtCpfKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_edtCpfKeyTyped
        char ch = evt.getKeyChar();
        try {
            Integer.parseInt(ch + "");
        } catch (NumberFormatException ex) {
            evt.consume();
        }
    }//GEN-LAST:event_edtCpfKeyTyped

    private void edtTelefoneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_edtTelefoneKeyTyped
        char ch = evt.getKeyChar();
        try {
            Integer.parseInt(ch + "");
        } catch (NumberFormatException ex) {
            evt.consume();
        }
    }//GEN-LAST:event_edtTelefoneKeyTyped

    private void edtRgKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_edtRgKeyTyped
        char ch = evt.getKeyChar();
        try {
            Integer.parseInt(ch + "");
        } catch (NumberFormatException ex) {
            evt.consume();
        }
    }//GEN-LAST:event_edtRgKeyTyped

    private void edtNomeFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_edtNomeFocusGained
        edtNome.selectAll();
    }//GEN-LAST:event_edtNomeFocusGained

    private void edtCpfFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_edtCpfFocusGained
        edtCpf.selectAll();
    }//GEN-LAST:event_edtCpfFocusGained

    private void edtEnderecoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_edtEnderecoFocusGained
        edtEndereco.selectAll();
    }//GEN-LAST:event_edtEnderecoFocusGained

    private void edtBairroFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_edtBairroFocusGained
        edtBairro.selectAll();
    }//GEN-LAST:event_edtBairroFocusGained

    private void edtCidadeFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_edtCidadeFocusGained
        edtCidade.selectAll();
    }//GEN-LAST:event_edtCidadeFocusGained

    private void edtTelefoneFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_edtTelefoneFocusGained
        edtTelefone.selectAll();
    }//GEN-LAST:event_edtTelefoneFocusGained

    private void edtRgFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_edtRgFocusGained
        edtRg.selectAll();
    }//GEN-LAST:event_edtRgFocusGained

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PnlClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PnlClientes().setVisible(true);
            }
        });
    }

    /**
     * Limpa os campos da tela
     *
     * @author Kevlin Toshinari Ossada - 30/01/2013 -
     * kevlin.ossada@fatec.sp.gov.br
     */
    private void limparTela() {
        lblId.setText("");
        edtBairro.setText("");
        edtCidade.setText("");
        edtCpf.setText("");
        edtEndereco.setText("");
        edtNome.setText("");
        edtTelefone.setText("");
        edtRg.setText("");
    }

    /**
     * Pega valores da linha selecionada e preenche nos campos da tela
     *
     * @author Kevlin Toshinari Ossada - 30/01/2013 -
     * kevlin.ossada@fatec.sp.gov.br
     */
    private void selecionaRegistro() {
        int rowSelected = tblClientes.getSelectedRow();

        lblId.setText(tblClientes.getValueAt(rowSelected, 0).toString());
        edtNome.setText(tblClientes.getValueAt(rowSelected, 1).toString());
        edtCpf.setText(tblClientes.getValueAt(rowSelected, 2).toString());
        edtEndereco.setText(tblClientes.getValueAt(rowSelected, 3).toString());
        edtBairro.setText(tblClientes.getValueAt(rowSelected, 4).toString());
        edtCidade.setText(tblClientes.getValueAt(rowSelected, 5).toString());
        edtTelefone.setText(tblClientes.getValueAt(rowSelected, 6).toString());
        edtRg.setText(tblClientes.getValueAt(rowSelected, 7).toString());
    }

    /**
     * Verifica se todos os campos obrigatorios foram preenchidos
     *
     * @return true/false
     *
     * @author Kevlin Toshinari Ossada - 30/01/2013 -
     * kevlin.ossada@fatec.sp.gov.br
     */
    private Boolean validarCampos() {
        return (!Utils.isNullOrEmpty(edtBairro.getText()))
                && (!Utils.isNullOrEmpty(edtCidade.getText()))
                && (!Utils.isNullOrEmpty(edtCpf.getText()))
                && (!Utils.isNullOrEmpty(edtEndereco.getText()))
                && (!Utils.isNullOrEmpty(edtNome.getText()))
                && (!Utils.isNullOrEmpty(edtTelefone.getText()));
    }
    
    /**
     * Limpa os dados da tabela
     *
     * @author Kevlin Toshinari Ossada - 30/01/2013 -
     * kevlin.ossada@fatec.sp.gov.br
     */
    private void limparTabela() {
        ((MeuTableModel) tblClientes.getModel()).setNumRows(0);
        tblClientes.updateUI();
    }

    /**
     * Preenche JTable com o retorno da query que busca todos os clientes
     *
     * @author Kevlin Toshinari Ossada - 30/01/2013 -
     * kevlin.ossada@fatec.sp.gov.br
     */
    private void preencherTabela() {
        limparTabela();

        ArrayList<ClienteBE> lstClientes = ClienteBO.getInstance().listarClientes();
        MeuTableModel model;
        try {
            model = (MeuTableModel) tblClientes.getModel();

            for (ClienteBE oCliente : lstClientes) {
                model.addRow(new String[]{
                            String.valueOf(oCliente.getCodigoCliente()),
                            oCliente.getNomeCliente(),
                            String.valueOf(oCliente.getCpfCliente()),
                            oCliente.getEnderecoCliente(),
                            oCliente.getBairroCliente(),
                            oCliente.getCidadeCliente(),
                            oCliente.getTelefoneCliente(),
                            oCliente.getRgCliente()
                        });
            }

            tblClientes.setModel(model);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());

        }
    }

    /**
     * Metodo que trata o estado em que o CRUD dos clientes se encontram
     *
     * @param estado Estado Crud
     *
     * * @author Kevlin Toshinari Ossada - 31/01/2013 -
     * kevlin.ossada@fatec.sp.gov.br
     */
    private void habilitarBotoes(EstadosBotoes estado) {
        if (estado.equals(EstadosBotoes.INSERIR)) {
            btnInserir.setEnabled(false);
            btnDeletar.setEnabled(false);
            btnCancelar.setEnabled(true);
            btnCadastrar.setEnabled(true);
            btnCadastrar.setText(EstadosBotoes.CADASTRAR.toString());
            lblId.setText(String.valueOf(ClienteBO.getInstance().getNextId()));
            habilitarDesabilitaCampos(true);
            edtNome.requestFocus();
        }
        if (estado.equals(EstadosBotoes.CADASTRAR)) {
            btnInserir.setEnabled(true);
            btnDeletar.setEnabled(false);
            btnCancelar.setEnabled(false);
            btnCadastrar.setEnabled(false);
            limparTela();
            preencherTabela();
            habilitarDesabilitaCampos(false);
        } else if (estado.equals(EstadosBotoes.DELETAR)) {
            btnInserir.setEnabled(true);
            btnDeletar.setEnabled(false);
            btnCancelar.setEnabled(false);
            btnCadastrar.setEnabled(false);
            limparTela();
            preencherTabela();
            habilitarDesabilitaCampos(false);
        } else if (estado.equals(EstadosBotoes.CANCELAR)) {
            btnInserir.setEnabled(true);
            btnDeletar.setEnabled(false);
            btnCancelar.setEnabled(false);
            btnCadastrar.setEnabled(false);
            limparTela();
            habilitarDesabilitaCampos(false);
        } else if (estado.equals(EstadosBotoes.ALTERAR)) {
            btnInserir.setEnabled(false);
            btnDeletar.setEnabled(true);
            btnCancelar.setEnabled(true);
            btnCadastrar.setEnabled(true);
            btnCadastrar.setText(EstadosBotoes.ALTERAR.toString());
            selecionaRegistro();
            habilitarDesabilitaCampos(true);
            edtNome.requestFocus();
        } else if (estado.equals(EstadosBotoes.INICIAL)) {
            btnInserir.setEnabled(true);
            btnDeletar.setEnabled(false);
            btnCancelar.setEnabled(false);
            btnCadastrar.setEnabled(false);
            limparTela();
            preencherTabela();
            habilitarDesabilitaCampos(false);
        }
    }

    /**
     * Habilita os desabilita text box's
     *
     * @param ativado Variavel de controle
     *
     * * @author Kevlin Toshinari Ossada - 31/01/2013 -
     * kevlin.ossada@fatec.sp.gov.br
     */
    private void habilitarDesabilitaCampos(Boolean ativado) {
        edtBairro.setEnabled(ativado);
        edtCidade.setEnabled(ativado);
        edtCpf.setEnabled(ativado);
        edtEndereco.setEnabled(ativado);
        edtNome.setEnabled(ativado);
        edtRg.setEnabled(ativado);
        edtTelefone.setEnabled(ativado);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnDeletar;
    private javax.swing.JButton btnInserir;
    private javax.swing.JTextField edtBairro;
    private javax.swing.JTextField edtCidade;
    private javax.swing.JFormattedTextField edtCpf;
    private javax.swing.JTextField edtEndereco;
    private javax.swing.JTextField edtNome;
    private javax.swing.JTextField edtRg;
    private javax.swing.JTextField edtTelefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JLabel lblId;
    private javax.swing.JTable tblClientes;
    // End of variables declaration//GEN-END:variables
}
